from flask import Flask
from flask_migrate import Migrate
from config import DevConfig
from db_init import db
from swaager_init import siwa
from cache import locale_cache
from tools.loging import login_manager
from logging.config import dictConfig
import logging

dictConfig({
    'version': 1,
    'formatters': {'default': {
        'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
    }},
    'handlers': {'wsgi': {
        'class': 'logging.StreamHandler',
        'stream': 'ext://flask.logging.wsgi_errors_stream',
        'formatter': 'default'
    }},
    'root': {
        'level': 'INFO',
        'handlers': ['wsgi']
    },
    'filename':'err.log'
})


def create_flask():
    # app = Flask(__name__, static_url_path='192.168.43.136@root:12324:/data/asdfasdf')
    app = Flask(__name__)
    login_manager.init_app(app)
    app.config.from_object(DevConfig)
    app.secret_key = 'asdfasdfasdf'
    migrate = Migrate()
    db.init_app(app=app)
    migrate.init_app(app, db)
    siwa.init_app(app)
    locale_cache.init_app(app, config={'CACHE_TYPE': 'SimpleCache'})
    return app
